// QueueUserAPC.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <process.h>
#include <iostream>
#include <fstream>

using namespace std;

ofstream fs;

VOID CALLBACK APCProc(
	__in  ULONG_PTR dwParam
	)
{
	fs << *(int *)dwParam << endl;
	cout << *(int *)dwParam << endl;
	fs.flush();
	Sleep(2000);
}

void threadproc(void *)
{
	int i = 1;
	QueueUserAPC(APCProc, GetCurrentThread(), (ULONG_PTR)&i);
	cout << SleepEx(2000, true) << endl;
	fs << 2 << endl;
	cout << 2 << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	//for (int i = 0; i < 1000; i++)
	{
		_beginthread(threadproc, 0, NULL);
	}

	//SleepEx(2000, true);

	getchar();

	fs.flush();
	fs.close();

	return 0;
}

